#include <iostream>
#include "bookdao_impl.h"
#include <unistd.h>
#include <stdio.h>
#include "book.h"

using namespace std;

void BookDaoImpl::load(vector<Book>& books)
{
	FILE* frp = fopen("account/books.dat","rb");
	if(frp == NULL)
	{
		return;
	}
	books.clear();	
	int ret =1; 
	while(ret)
	{
		Book* book= new Book;
		ret =fread(book,sizeof(Book),1,frp);
		if(ret)		
		books.push_back(*book);	
	}
	fclose(frp);
	return;
}

void BookDaoImpl::save(vector<Book>& books)
{
	FILE* fwp = fopen("account/books.dat","wb");
	if(fwp == NULL)
	{
		perror("fopen");
		return;
	}
	
	for(unsigned int i=0; i<books.size(); i++)
	{
		fwrite(&books[i],1,sizeof(Book),fwp);
	}
	fclose(fwp);
}
